package com.attilax.wechat;

/**
 * 
 */

//import org.junit.Test;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.sikuli.script.*;

import com.attilax.io.filex;
import com.attilax.io.pathx_noWeb;
 

//import com.attilax.lang.core;

//import org.sikuli.script.ImagePath;

/**
 * @author ASIMO
 * 
 */
public class checkWechatReg {

	private static String imgpath;
	private static Logger log = Logger.getLogger(checkWechatReg.class);
	/**
	 * @author attilax 老哇的爪子
	 * @throws IOException 
	 * @throws FindFailed
	 * @since p34 j_55_h
	 */
	public static void main(String[] args) throws IOException {
	 	mainProcess(args);
		
		
		// System.load("C:\\Users\\ASIMO\\AppData\\Local\\Temp\\tmplib\\VisionProxy.dll");
		// System.out.println(Key.DELETE);
	

		// Match[489,648 158x70 score=0.79 target=center]
//		
//		filex fx=new filex("c:\\zz.txt");
//	 
//					fx.appendLine_flush_safe( "已经注册");
//				 
//		fx.close();

		System.out.println("--程序运行结束");

	}

	private static void mainProcess(String[] args) throws IOException {
		String f=args[0];
		String f2=args[1];
		List<String> li=filex.read2list_filtEmptyNstartSpace(f);
		filex fx=new filex(f2);
		for (String cp : li) {
			try {
				boolean rzt=checkregExist(cp);
				if(rzt)
					fx.appendLine_flush_safe(cp+",已经注册");
				else
					fx.appendLine_flush_safe(cp+",没注册");
			} catch (Exception e) {
				e.printStackTrace();
				log.error(cp, e);
				
			}
			
				
		}
		fx.close();
	}

		/**
		@author attilax 老哇的爪子
		@since   p35 f_j_8
		 
		 */
	private static boolean checkregExist(String cp) {
		Screen s = new Screen();
		// s.set
		// ImagePath.add("C:\\111");
		// s.click("C:\\111\\20150304175920.png");
		// s.doubleClick("C:\\111\\20150304175920.png");
		// s.type(arg0, arg1, arg2)
		// s.find(arg0)
		// s.paste(arg0, arg1)+
		imgpath=pathx_noWeb.AppPath()+"\\img\\";
		System.out.println(imgpath);
		String edit = imgpath+"edit2.png";
		// try {
		// Match mtc=s.find(edit);
		// System.out.println(mtc);
		// } catch (FindFailed e2) {
		// // TODO Auto-generated catch block
		// e2.printStackTrace();
		// }
		// s.type(edit,"a",KeyModifier.CTRL);
		try {
			String rigkeys = "";
			String BACKSPACEss = "";
			for (int i = 0; i < 11; i++) {
				rigkeys = rigkeys + Key.RIGHT;
				BACKSPACEss = BACKSPACEss + Key.BACKSPACE;
			}
			s.click(edit);
			s.type(edit, rigkeys);
			s.type(BACKSPACEss);
			// 13764305367
//			String notExistCp = "13753664068";
//			String existCp = "13764305366";
			s.type(imgpath+"edit2.png", cp);
			s.click(imgpath+"search.png");
		} catch (Exception e) {
			//e.printStackTrace();
			throw new RuntimeException(" search error ",e);
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		// core.sleep();

		if (sucess())
		{
			System.out.println("---user  exist " +cp);
			
			try {
				s.click(imgpath+"toMainAftCheckExist.png");
			} catch (FindFailed e) {
			 
				throw new RuntimeException(e);
			}
			
			return true;
		}

		else {

			try {
				System.out.println("---user is not exist "+cp);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				s.click(imgpath+"yes.png");
				return false;
			} catch (FindFailed e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw new RuntimeException(e1);
			}
		}
		//return false;
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since p35 0_z_42
	 */
	private static boolean sucess() {
		Screen s = new Screen();
    while(true)
    {
		if (Exist(imgpath+"add2telbook.png"))
			return true;
		if (Exist(imgpath+"sendmsg.png"))
			return true;
		if (Exist(imgpath+"userNotExist.png"))
			return false;
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

		//return false;
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since p35 0_39_48
	 */
	private static boolean Exist(String f) {
		Screen s = new Screen();
		Match m = null;
		try {
			Pattern ptt = new Pattern(f);
			ptt.similar(0.9f);
			m = s.find(ptt);
			System.out.println(m);
			if (m.getScore() < 0.9)
				throw new FindFailed("");
			return true;

		} catch (FindFailed e) {
			return false;

		}
		// return false;
	}

}
